﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Sayeet
{
    public partial class AddContactWindow : Form
    {
        
        public List<string> groups;
        public AppData appData;
        public AddContactWindow()
        {
            groups = new List<string>();
            InitializeComponent();
        }

        public void reloadGroups(List<string> groups)
        {
            groupCombo.Items.Clear();
            foreach (string group in groups)
            {
                groupCombo.Items.Add(group);
            }
            groupCombo.Items.Add("--Add new group--");
            groupCombo.SelectedIndex = 0;
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            if (jidTextBox.Text != "")
            {
                RosterItem newContact = new RosterItem(new JID(jidTextBox.Text), aliasTextBox.Text, groupCombo.SelectedItem.ToString(), appData);
                appData.newContact(newContact);
                appData.addContact(newContact);
            }
            Close();
        }

        private void AddContactWindow_Load(object sender, EventArgs e)
        {
            reloadGroups(groups);
        }

        private void groupCombo_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(groupCombo.SelectedIndex == groupCombo.Items.Count-1)
            {
                AddGroupWindow dialog = new AddGroupWindow();
                if(dialog.ShowDialog() == DialogResult.OK)
                {
                    groups.Add(dialog.newGroup);
                    reloadGroups(groups);
                    groupCombo.SelectedItem = groups[groups.Count - 1];
                }
            }
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
